//
//  FZUserAcoount.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/29.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit
/*
 "access_token" = "2.00QEEtRE2fvmFEb4d44bb93dLK3TOB";
 "expires_in" = 157679999;
 "remind_in" = 157679999;
 uid = 3928852720;
*///沙盒路径
let strPath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("userAccount.data") 

class FZUserAcoount: NSObject,NSCoding{

    var access_token : String?
    var expires_date : Date?
    var expires_in : Int = 0 {
        
        didSet {
            expires_date = Date().addingTimeInterval(TimeInterval(expires_in))
        }
    }
    var uid : Int64 = 0
    
    var name : String?
    
    var avatar_large : String?
    
    init(dic : [String : Any]) {
        super.init()
        setValuesForKeys(dic)
        
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        uid = aDecoder.decodeInt64(forKey: "uid")
        name = aDecoder.decodeObject(forKey: "name") as! String?
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        print("https://api.weibo.com/2/statuses/home_timeline.json?access_token=\(access_token!)")
    }
    
    func saveUserAccount() {
        
        NSKeyedArchiver.archiveRootObject(self, toFile: strPath as String)
    }
    
   class func getUserAcount() -> FZUserAcoount? {
    
     let data = NSKeyedUnarchiver.unarchiveObject(withFile: strPath as String)
     return data as? FZUserAcoount
    }
    
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
}
